package org.soretro.portfolio.helpers
{
	import com.google.analytics.AnalyticsTracker;
	import com.google.analytics.GATracker;
	
	import flash.net.LocalConnection;
	
	import mx.managers.SystemManager;
	
	public class GoogleAnalytics {
		
		private static var instance:GoogleAnalytics;
		private static var allowInstantiation:Boolean;
		private static var tracker:AnalyticsTracker;
		
		public static function getInstance():GoogleAnalytics {
			if (instance == null) {
				allowInstantiation = true;
				instance = new GoogleAnalytics();
				allowInstantiation = false;
			}
			return instance;
		}
		
		public function googleTracker():void
		{
			tracker = new GATracker( SystemManager.getSWFRoot(this).stage, "UA-18699334-1", "AS3", false );
		}
		
		public function send( name:String ):void 
		{
			googleTracker();
			var lc:LocalConnection = new LocalConnection();
			
			if ( lc.domain.toString() != "localhost") {
				trace( "Tracking = "+ name );
				tracker.trackPageview( name );
			}
		}
		
		public function GoogleAnalytics():void {
			if (!allowInstantiation) {
				throw new Error("Error: Instantiation failed: Use GoogleAnalytics.getInstance() instead of new.");
			}
		}
	}
}

/*
package org.soretro.portfolio.helpers
{
	import com.google.analytics.AnalyticsTracker;
	import com.google.analytics.GATracker;
	
	import flash.net.LocalConnection;
	
	import mx.managers.SystemManager;

	public class GoogleAnalytics
	{
		static protected var tracker:AnalyticsTracker;
		
		public function GoogleAnalytics()
		{
			googleTracker();
		}
		
		public function googleTracker():void
		{
			tracker = new GATracker( SystemManager.getSWFRoot(this).stage, "UA-18699334-1", "AS3", false );
		}
		
		static public function send( name:String ):void 
		{
			
			trace( "Tracking = "+ name );
			var lc:LocalConnection = new LocalConnection();
			
			if ( lc.domain.toString() == "localhost") {
				tracker.trackPageview( name );
			}
		}
	}
}
*/